// 处理css
// import csshook from 'css-modules-require-hook/preset';
// 处理图片
// import assethook from 'asset-require-hook';
assethook({
  extensions: ['png', 'jpg']
});

const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
// const userRoute = require("./userRoute");
const app = express();
const path = require('path');
app.use(cookieParser());
app.use(bodyParser.json());

/**
 * 插入react代码 进行服务端改造
 */
import React from 'react';
import ReactDOM from 'react-dom';
import {
  createStore,
  applyMiddleware,
  compose
} from "redux";
import { Provider } from "react-redux";
import thunk from "redux-thunk";
// 引入antd css
import 'antd-mobile/dist/antd-mobile.css';
// 引入renderToString
import { renderToString, renderToNodeStream } from 'react-dom/server';
// 服务端是没有BrowserRouter 所以用StaticRouter
import { StaticRouter } from "react-router-dom";
// 引入reducer
import reducers from "../src/reducer";
// 引入前端路由
import Routers from '../src/router';
// 引入css 和 js
import buildPath from '../build/asset-manifest.json';

const store = createStore(reducers, compose(
  applyMiddleware(thunk),
));

// 映射到build后的路径
//设置build以后的文件路径 项目上线用
app.use((req, res, next) => {
  if (req.url.startsWith('/user/') || req.url.startsWith('@STATIC/')) {
    return next()
  }

  const context = {}
  const frontComponents = renderToNodeStream(
    (<App />)
  )
  res.send(frontComponents)
})
app.use('/', express.static(path.resolve('build')))

app.listen("4000",function(){
  console.log("open Browser http://localhost:4000");
});
